/**
 * @Classname Dynamic 动态规划算法
 * @Description 将问题拆分为子问题，但是子问题又不是相互独立的 → 上一个子阶段的基础上进行下一步的求解
 * 经典案例：背包问题
 * @Date 2021/12/23 14:05
 */
public class Dynamic {
    public static void main(String[] args) {
        int[] w  = {1,4,3}; //物品重量
        int[] val = {1500,3000,2000};//物品价值
        int m = 4; //背包容量
        int n = val.length; // 物品个数

        int[][] v = new int[n+1][m+1];
        int[][] path = new int[n+1][m+1];
        for (int i=0;i<v.length;i++ ){
            v[i][0] =0;
        }
        for (int i=0;i<v[0].length;i++ ){
            v[0][i] =0;
        }

        for (int i=1;i<v.length;i++ ){
            for (int j=1;j<v[0].length;j++ ) {
                if (w[i-1]>j){
                    v[i][j] = v[i-1][j];
                }else {
                    if ( v[i-1][j] <val[i-1] +  v[i-1][j-w[i-1]]){
                        v[i][j] = val[i-1] +  v[i-1][j-w[i-1]];
                        path[i][j] = 1;
                    }else {
                        v[i][j] = v[i-1][j];
                    }
                }

            }
        }

        for (int i=0;i<v.length;i++ ){
            for (int j=0;j<v[0].length;j++ ) {
                System.out.println(v[i][j]+"");
            }
            System.out.println();
        }
        System.out.println("======================");

        int i = path.length -1;
        int j = path[0].length -1;
        while (i>0 && j>0){
            if (path[i][j] ==1){
                System.out.printf("第%d个商品放入到背包中\n",i);
                j-= w[i-1];
            }
            i--;
        }
    }
}
